<?php
/*************
#系统菜单管理
#lsj 2010-12-13
*************/

class sysmenuCtrl extends lib_ctrl
{
    public $Cname = '菜单管理';
    public $menu = null;#菜单对象
    
    /*
     * 初始化
     */
    public function _init()
    {
        parent::_init();
        $this->menu = $this->loadModel('menu');
    }
    
    /*
     * 菜单列表，不用分页，树状列表显示
     */
    public function indexAct()
    {
        $result = $this->menu->getMenu();
        if($result){
                $res = $this->menu->formatTree($this->menu->formatFather($result));
        }

        $this->view->assign('res', $res);
        $this->view->display();
    }
	
	/*
	 * 添加菜单，AJAX提交
	 */
	public function addAct()
	{
		if(!empty($_POST['item'])){
			$item = $this->menu->filterItem($_POST['item']);
			if('' == $item['name']) exit('-10');#菜单名称不能为空
			if('' == $item['ctrl']) exit('-11');#控制器名称不能为空
			if('' == $item['act']) exit('-12');#动作名称不能为空
			
			$item['url'] = str_replace(DOMAIN_ROOT . '/', '', $this->getURL($item['ctrl'], $item['act']));
			
			$res = $this->menu->addMenu($item);#入库
			if($res) exit('1');#成功
			exit($res);#失败
		}else{
			$result = $this->menu->getMenu();#获取已存在的菜单树
			if($result){
				$tree = $this->menu->formatTree($this->menu->formatFather($result));
			}
			$ctrls = $this->menu->getCtrls();#取出所有的控制器名称
			$mtypes = $this->menu->getMtypes();#取出菜单类型的备选项
			
			$this->view->assign('tree', $tree);
			$this->view->assign('ctrls', $ctrls);
			$this->view->assign('mtypes', $mtypes);
		}
		
		
		$this->view->display();
	}
	
	/*
	 * 批量添加菜单，处理成功后跳转到列表页，AJAX提交
	 */
	public function addsAct()
	{
		if(!empty($_POST['item'])){
			$ok = 0;
			foreach($_POST['item'] as $key=>$val){
				$item = $this->menu->filterItem($val);
				if('' == $item['name']) exit('-'.($key+1).'10');#第N条的菜单名称不能为空
				if('' == $item['ctrl']) exit('-'.($key+1).'11');#第N条的控制器名称不能为空
				if('' == $item['act']) exit('-'.($key+1).'12');#第N条的动作名称不能为空
				
				$item['url'] = str_replace(DOMAIN_ROOT . '/', '', $this->getURL($item['ctrl'], $item['act']));
			
				$res = $this->menu->addMenu($item);#入库
				if($res) $ok++;#成功条数
			}
			exit($ok);#返回成功的条数
		}else{
			$result = $this->menu->getMenu();#获取已存在的菜单树
			if($result){
				$tree = $this->menu->formatTree($this->menu->formatFather($result));
			}
			$ctrls = $this->menu->getCtrls();#取出所有的控制器名称
			$mtypes = $this->menu->getMtypes();#取出菜单类型的备选项
			
			$this->view->assign('tree', $tree);
			$this->view->assign('ctrls', $ctrls);
			$this->view->assign('mtypes', $mtypes);
		}

		$this->view->display();
	}
	
	/*
	 * 修改菜单，单条修改，AJAX提交
	 */
	public function editAct()
	{
		if(!empty($_POST['item'])){
			$item = $this->menu->filterItem($_POST['item']);
			if(!$item['id']) exit('-10');#缺少菜单ID
			if('' == $item['name']) exit('-11');#菜单名称不能为空
			if('' == $item['ctrl']) exit('-12');#控制器名称不能为空
			if('' == $item['act']) exit('-13');#动作名称不能为空
			
			$item['url'] = str_replace(DOMAIN_ROOT . '/', '', $this->getURL($item['ctrl'], $item['act']));
			
			$res = $this->menu->editMenu($item);#入库
			if($res) exit('1');#成功
			exit($res);#失败
		}else{
			$id = abs(intval($_GET['id']));#要修改的菜单的ID
			$nowmenu = $this->menu->getMenu($id);#获取要修改的菜单的信息
			$result = $this->menu->getMenu();#获取已存在的菜单树
			if($result){
				$tree = $this->menu->formatTree($this->menu->formatFather($result));
			}
			$ctrls = $this->menu->getCtrls();#取出所有的控制器名称
			$mtypes = $this->menu->getMtypes();#取出菜单类型的备选项
			
			$this->view->assign('nowmenu', $nowmenu);
			$this->view->assign('tree', $tree);
			$this->view->assign('ctrls', $ctrls);
			$this->view->assign('mtypes', $mtypes);
		}
		
		$this->view->display();
	}
	
	/*
	 * 删除菜单，可以批量，可单条 ，AJAX提交
	 */
	public function delAct()
	{
		$id = abs(intval($_POST['id']));#单条菜单的ID
		$ids = (array)$_POST['ids'];#批量菜单的ID们
		
		if($id){
			if($this->menu->setFid($id)->getMenu()) exit('-10');#存在子菜单不能删除
			$res = $this->menu->delMenu($id);#从库中删除
			if($res) exit('1');#成功
			exit($res);#失败
		}elseif(!empty($ids)){
			$ok = 0;
			foreach($ids as $key=>$val){
				$id = abs(intval($val));
				if($this->menu->setFid($id)->getMenu()) break;#存在子菜单不能删除
				$res = $this->menu->delMenu($id);#从库中删除
				if($res) $ok++;#成功
			}
			exit($ok);#返回成功条数
		}
		
		eixt('0');#缺少菜单ID
	}
	
	/*
	 * 有层级地列出所有的父级菜单, AJAX提交
	 */
	public function listfatherAct()
	{
		$father = array();
		$result = $this->menu->getMenu();#获取已存在的菜单树
		
		if($result){
			$tree = $this->menu->formatTree($this->menu->formatFather($result));
			$id = abs(intval($_GET['id']));#菜单的ID
			if($id) $nowmenu = $this->menu->getMenu($id);#取出当前菜单的信息
			foreach($tree['order'] as $index=>$one){
				$name = '';
				$sel = 0;
				if($nowmenu && $nowmenu['fid'] == $tree['menu'][$one]['id']) $sel = 1;	
				if($tree['deep'][$index] != 1){
					if(!isset($tree['deep'][$index+1]) || $tree['deep'][$index+1] < $tree['deep'][$index]){
						$name .= '└';
					}else{
						$name .= '├';
					}
					$name .= str_repeat(' -', $tree['deep'][$index] - 2);
				}
				$name .= $tree['menu'][$one]['name'] . '&nbsp;(' . $one . ')';
				$father[] = array('id'=>$tree['menu'][$one]['id'], 'sel'=>$sel, 'name'=>$name);
			}
		}
		
		exit(json_encode($father));
	}
	
	/*
	 * 列出所有的控制器, AJAX提交
	 */
	public function listcontrolAct()
	{
		$ctrls = array();
		$cts = $this->menu->getCtrls();#取出所有的控制器名称
		if($cts){
			$id = abs(intval($_GET['id']));#菜单的ID
			if($id) $nowmenu = $this->menu->getMenu($id);#取出当前菜单的信息
			foreach($cts as $index=>$one){
				$sel = 0;
				if($nowmenu && $nowmenu['ctrl'] == $one['name']) $sel = 1;
				$ctrls[] = array('sel' => $sel, 'name' => $one['name'], 'num'=> $one['num']);
			}
		}
		
		exit(json_encode($ctrls));
	}
	
	/*
	 * 列出对应控制器中所有的动作, AJAX提交
	 */
	public function listactionAct()
	{
		$ctrl = strip_tags(trim($_GET['cn']));
		if($ctrl != ''){
			$acts = array();
			$ts = $this->menu->getActs($ctrl);
			if($ts){
				$id = abs(intval($_GET['id']));#菜单的ID
				if($id) $nowmenu = $this->menu->getMenu($id);#取出当前菜单的信息
				foreach($ts as $index=>$one){
					$name = substr($one, 0, -3);
					$sel = 0;
					if($nowmenu && $nowmenu['act'] == $one) $sel = 1;
					$acts[] = array('sel' => $sel, 'name' => $one);
				}
			}
			exit(json_encode($acts));
		}
	}

	
	







}


?>